package com.autumn.demo.scheduler.queue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.autumn.mq.event.EventHandlerConfigure;
import com.autumn.mq.event.EventMessage;
import com.autumn.mq.event.IEventHandler;
import com.autumn.scheduler.api.QueueJobEventData;
import com.autumn.scheduler.api.QueueSchedulerStatus;
import com.autumn.scheduler.api.services.AutumnJobService;

@Component
@EventHandlerConfigure(name = QueueJobEventData.JOB_QUEUE_PREFIX + "task_2", concurrentConsumers = 10)
public class Task2 implements IEventHandler<QueueJobEventData> {

	private final static Log logger = LogFactory.getLog(Task1.class);

	@Override
	public boolean errorAutoRepeatSend() {
		return false;
	}

	@Override
	public Log getLogger() {
		return logger;
	}

	@Reference
	private AutumnJobService client;

	@Override
	public void onHandler(EventMessage<QueueJobEventData> message) {
		long beginTime = System.currentTimeMillis();
		System.out.println("-----------任务来了--------------  " + message.getData().getJobName() + " -----------------");
		System.out.println(message.getBodyString());
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long endTime = System.currentTimeMillis();
		QueueSchedulerStatus status = QueueSchedulerStatus.successResult(message.getData(), beginTime, endTime,
				"task执行成功了");
		client.updateJobStatus(status);
	}
}
